package com.example.schoolenterprisealliance.fragment;
//最新动态

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.schoolenterprisealliance.R;
import com.example.schoolenterprisealliance.activity.DynamicActivity;
import com.example.schoolenterprisealliance.activity.ItemActivity;
import com.example.schoolenterprisealliance.adapter.New_InformationAdapter;
import com.example.schoolenterprisealliance.bean.NewIfomation;
import com.example.schoolenterprisealliance.dialog.CancelDialog;
import com.example.schoolenterprisealliance.io.AppConfig;
import com.example.schoolenterprisealliance.service.DynamicService;
import com.example.schoolenterprisealliance.time.Time;

import java.util.List;

public class DynamicFragment extends Fragment {
    private New_InformationAdapter new_informationAdapter;
    private RecyclerView recyclerView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_dynamic, container, false);
        recyclerView=view.findViewById(R.id.rv_new_information);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        //添加Android自带的分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);

        informationData();
        return view;
    }
    //获取最新资讯数据
    private void informationData() {
        //1, 声明Retrofit 的构建者
        Retrofit.Builder builder = new Retrofit.Builder();
        //2, 设置公共的地址
        builder.baseUrl(AppConfig.BASE_URL);
        //3, 设置解析gson
        builder.addConverterFactory(GsonConverterFactory.create());
        //4, 得到Retrofit 对象
        Retrofit retrofit = builder.build();
        //5, 得到接口
        DynamicService dynamicService = retrofit.create(DynamicService.class);
        //6, 得到Call 的方法
        Call<NewIfomation> call = dynamicService.getInformationData();
        //7, 传入接口回调 , 获取数据
        call.enqueue(new Callback<NewIfomation>() {
            @Override
            public void onResponse(Call<NewIfomation> call, Response<NewIfomation> response) {
                //获得数据
                NewIfomation newIfomation = response.body();
                List<NewIfomation.DataBean> data=newIfomation.getData();
                new_informationAdapter =new New_InformationAdapter(R.layout.item_new_information,data,getActivity());
                recyclerView.setAdapter(new_informationAdapter);
                //设置跳转监听
                new_informationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent =new Intent(getActivity(), ItemActivity.class);
                        String title=data.get(position).getTitle();
                        String time= Time.longToString(data.get(position).getTime());
                        String context=data.get(position).getContext();
                        intent.putExtra("title",title);
                        intent.putExtra("time",time);
                        intent.putExtra("context",context);
                        startActivity(intent);
                        Log.e("position",position+"");
                    }
                });
                //对话框点击确认时根据传回的position移除数据
                new_informationAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                        switch (view.getId())
                        {
                            case R.id.iv_cancel:
//                                Log.e("cancel","cancel图片被点击了");
                                CancelDialog cancelDialog=new CancelDialog(getActivity(), R.layout.content_cancel_dialog,
                                        position,new CancelDialog.PriorityListener() {
                                    @Override
                                    public void setActivityText(int position, String string) {
                                        if (string.equals("确认"))
                                            data.remove(position);
                                        Log.e("iv_cancel","CancelDialog点击确认了");
                                        adapter.setNewData(data);
                                    }
                                });
                                Window window= cancelDialog.getWindow();
                                cancelDialog.getWindow().setLayout(350,300);
                                cancelDialog.show();
                                cancelDialog.setCancelable(false);
                                break;
                        }
                    }
                });
            }

            @Override
            public void onFailure(Call<NewIfomation> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}
